<?php

namespace app\http\middleware;

use util\jwt\Token as tokenService;
//use app\api\service\Token as tokenService;

class Token
{
//    protected $header = [
//        'Access-Control-Allow-Origin' => '*',
//        'Access-Control-Allow-Headers' => 'Authori-zation,Authorization, token,Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
//        'Access-Control-Allow-Methods' => 'GET,POST,PATCH,PUT,DELETE,OPTIONS,DELETE',
//    ];

    public function handle($request, \Closure $next)
    {
        // 获取头部信息
        $token = trim($request->header('Authorization'));


        if (!$token || substr($token,0,7)!=="Bearer ") return json(['code'=>0,'msg'=>'无效令牌！']);
        $TokenValue = tokenService::verifyToken(substr($token,7));
        if (!$TokenValue){
            return json(['code'=>301,'msg'=>'Token令牌失效！']);
        }
        $TokenValue = json_decode($TokenValue);
        defined('COMID') or define('COMID', $TokenValue->apiid);//保存公司id 为常量
        return $next($request);
    }
}
